package org.spring.oss;


import com.google.common.net.UrlEscapers;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.stream.Collectors;

public interface IOss {
    default String encodeName(String origin){
        String[] names=origin.split("/");
        return Arrays.stream(names).map((s)-> UrlEscapers.urlPathSegmentEscaper().escape(s))
                .collect(Collectors.joining("/"));
    }

    OssConfig getConfig();
    void setConfig(OssConfig config);
    boolean uploadStream(String bucket,String name,String filename,InputStream inputStream,long size);
    default boolean uploadStream(String name,String filename,InputStream inputStream,long size){
        return uploadStream(getConfig().getBucket(),name,filename,inputStream,size);
    }
    default boolean uploadFile(String bucket, File file){
        try(FileInputStream fileInputStream=new FileInputStream(file)){
            return uploadStream(bucket, file.getName(), file.getName(), fileInputStream, file.length());
        }
        catch (Exception e){
            return false;
        }
    }
    default boolean uploadFile(File file){
        return uploadFile(getConfig().getBucket(),file);
    }
    String buildDownloadUrl(String bucket, String name, long validTime);
    default String buildDownloadUrl(String name, long validTime){
        return buildDownloadUrl(getConfig().getBucket(), name, validTime);
    }
    default String buildDownloadUrl(String bucket, String name){
        return buildDownloadUrl(bucket,name,20);
    }
    default String buildDownloadUrl(String name){
        return buildDownloadUrl(name,20);
    }

    boolean bucketExists(String bucket);
    boolean objectExists(String bucket, String name);
    boolean deleteObject(String bucket, String name);

}
